import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  Put,
  UseGuards,
} from '@nestjs/common';
import { GroupService } from './group.service';
import { CreateGroupDto } from './dto/create-group.dto';
import { UpdateGroupDto } from './dto/update-group.dto';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { AuthGuard } from '@nestjs/passport';
import { QueryPageDto } from '../../common/dto/query.page.dto';
import { Prisma } from '@prisma/client';
import { QueryGroupDto } from './dto/query-group.dto';

@Controller('group')
@ApiTags('工序组管理')
@UseGuards(AuthGuard('jwt'))
@ApiBearerAuth()
export class GroupController {
  constructor(private readonly groupService: GroupService) {}

  @Post()
  create(@Body() createGroupDto: CreateGroupDto) {
    return this.groupService.createModel<CreateGroupDto>(createGroupDto);
  }

  @Post('page')
  findByPage(@Body() query: QueryPageDto<QueryGroupDto>) {
    const where: Prisma.groupWhereInput = {
      isDelete: false,
      name: {
        contains: query.model.name || undefined,
      },
    };

    return this.groupService.queryPage(query, where);
  }

  @Get()
  findAll() {
    return this.groupService.findAll({
      isDelete: false,
    });
  }

  @Get(':id')
  findOne(@Param('id') id: string) {
    return this.groupService.findOne(id);
  }

  @Put(':id')
  update(@Param('id') id: string, @Body() updateGroupDto: UpdateGroupDto) {
    return this.groupService.updateModel<UpdateGroupDto>(id, updateGroupDto);
  }

  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.groupService.delete(id);
  }
}
